﻿namespace SPWikiProvisioning.Helpers
{
    #region Usings

    using System;
    using System.Threading;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    using Model;

    #endregion

    internal class LocalizationHelper
    {
        public string GetLocalizedLiteral(string sourceAndFile)
        {
            return GetLocalizedLiteral(sourceAndFile, (uint)Thread.CurrentThread.CurrentUICulture.LCID);
        }

        public string GetLocalizedLiteralCurrentWebLanguage(string sourceAndFile)
        {
            if (!sourceAndFile.StartsWith("$Resources:", StringComparison.CurrentCultureIgnoreCase))
            {
                return sourceAndFile;
            }

            var localizableString = new LocalizableString(sourceAndFile);
            return GetLocalizedLiteral(localizableString.Source, localizableString.DefaultResourceFile, SPContext.Current.Web.Language);
        }

        public string GetLocalizedLiteral(string sourceAndFile, uint language)
        {
            if (!sourceAndFile.StartsWith("$Resources:", StringComparison.CurrentCultureIgnoreCase))
            {
                return sourceAndFile;
            }

            var localizableString = new LocalizableString(sourceAndFile);
            return GetLocalizedLiteral(localizableString.Source, localizableString.DefaultResourceFile, language);
        }

        public string GetLocalizedLiteral(string source, string defaultResourceFile)
        {
            if (SPContext.Current != null)
            {
                return GetLocalizedLiteral(source, defaultResourceFile, (uint)Thread.CurrentThread.CurrentUICulture.LCID);
            }

            return source;
        }

        public string GetLocalizedLiteral(string source, string defaultResourceFile, uint lenguage)
        {
            return SPUtility.GetLocalizedString(source, defaultResourceFile, lenguage);
        }
    }
}
